package gulimall.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gulimall.entity.Attr;
import gulimall.entity.AttrAttrgroupRelation;
import gulimall.entity.ProductAttrValue;
import gulimall.service.AttrAttrgroupRelationService;
import gulimall.service.AttrService;
import gulimall.service.ICategoryService;
import gulimall.service.ProductAttrValueService;
import gulimall.vo.AttrRespVo;
import gulimall.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import gulimall.utils.PageUtils;
import gulimall.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性
 *
 * @author HLF
 * @email 1132814637@qq.com
 * @date 2022-05-04 15:04:15
 */
@RestController
@RequestMapping("/product/attr")
public class AttrController {
    @Resource
    private AttrService attrService;

    @Resource
    private ICategoryService categoryService;

    @Resource
    private AttrAttrgroupRelationService relationService;

    @Resource
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     * attrType是属性类型：base表示规格参数，sale表示销售属性
     */
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Long catelogId,
                  @PathVariable String attrType) {
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     * 接口文档：/product/attr/info/{attrId}
     * {
     * "msg": "success",
     * "code": 0,
     * "attr": {
     * "attrId": 4,
     * "attrName": "aad",
     * "searchType": 1,
     * "valueType": 1,
     * "icon": "qq",
     * "valueSelect": "v;q;w",
     * "attrType": 1,
     * "enable": 1,
     * "showDesc": 1,
     * "attrGroupId": 1, //分组id ❗️
     * "catelogId": 225, //分类id
     * "catelogPath": [2, 34, 225] //分类完整路径❗️
     * }
     * }
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrRespVo);
    }


    /**
     * 获取spu规格
     * /product/attr/base/listforspu/{spuId}
     * 返回ProductAttrValue对象集合
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId") String spuId) {
        List<ProductAttrValue> list = productAttrValueService.list(
                new QueryWrapper<ProductAttrValue>()
                .lambda().eq(ProductAttrValue::getSpuId, spuId)
        );
        return R.ok().put("data", list);
    }

    /**
     * 修改商品规格参数
     * /product/attr/update/{spuId}
     */
    @PostMapping("/update/{spuId}")
    public R updateProductAttr(@PathVariable("spuId") String spuId,
                               @RequestBody List<ProductAttrValue> productAttrValues) {

        productAttrValueService.updateProductAttr(spuId,productAttrValues);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo) {
        attrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo) {

        attrService.updateAttrById(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
